#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidgetAction>
#include <QMainWindow>
#include <QCheckBox>
#include <QPushButton>
#include <QLabel>
#include <QFileDialog>
#include <QProgressBar>
#include <QTimer>
#include <QFileInfo>
#include <QDockWidget>
#include <QMenuBar>
#include <QStatusBar>
#include <QMenu>
#include <QAction>
#include <QMessageBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QGroupBox>
#include <QCheckBox>
#include <QPushButton>
#include <QLabel>
#include <QTimer>
#include <QToolBar>
#include <QInputDialog>
#include <QListWidget>
#include <QDialog>
#include <QSqlQuery>
#include <QFile>
#include <QApplication>
#include <QStyle>
#include <QStyleFactory>
#include <QRadioButton>
#include <QComboBox>
#include <QSpinBox>
#include <QDoubleSpinBox>
#include <QTabWidget>
#include <QLineEdit>
#include <QActionGroup>



#include "BioSignalDevice.h"
#include "DeviceMonitorPanel.h"
#include "ECGDeviceThread.h"
#include "GSRDevice.h"
#include "EEGDevice.h"
#include "ECGDevice.h"
#include "ChartWidget.h"
#include "ChatWidget.h"
#include "AudioPlayerWidget.h"
#include "AnalysisWidget.h"
#include "DatabaseManager.h"
#include "GSRDeviceThread.h"
#include "TextToSpeechManager.h"
#include "SubjectInfoDialog.h"

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    // 生理信号设备
    GSRDevice *m_gsrDevice;
    EEGDevice *m_eegDevice;
    ECGDevice *m_ecgDevice;
    // 界面模块部件
    ChartWidget *m_chartWidget;
    ChatWidget *m_chatWidget;
    AudioPlayerWidget *m_audioWidget;
    AnalysisWidget *m_analysisWidget;
    // 设备控制UI元素
    QCheckBox *m_eegCheckBox;
    QCheckBox *m_ecgCheckBox;
    QCheckBox *m_gsrCheckBox;
    QPushButton *m_connectButton;
    QPushButton *m_disconnectButton; // 新增断开设备按钮
    QPushButton *m_startAcquisitionButton;
    QPushButton *m_stopAcquisitionButton;
    QPushButton *m_newSubjectButton; // 新增被试按钮
    QPushButton *m_selectSubjectButton; // 选择被试按钮
    QLabel *m_currentSubjectLabel; // 当前被试信息标签
    // 刺激控制按钮
    QPushButton *m_startStimulusButton;
    QPushButton *m_stopStimulusButton;
    // 刺激进度条
    QProgressBar *m_stimulusProgressBar;
    QLabel *m_stimulusTimeLabel;

    // 数据库管理
    DatabaseManager *m_db;

    //设备管理
    DeviceMonitorPanel *m_deviceMonitorPanel;

    // 文字转语音管理器
    TextToSpeechManager *m_ttsManager;

    // 被试信息采集对话框
    SubjectInfoDialog *m_subjectInfoDialog;

    void setupUi();       // 初始化UI布局
    void setupDockWidgets(); // 创建并添加各Dock窗口
    void connectSignals();   // 连接各模块的信号和槽
    void showSubjectInfoDialog(); // 显示被试信息采集对话框
    void showSelectSubjectDialog(); // 显示选择被试对话框
    void updateCurrentSubjectLabel(); // 更新当前被试信息标签
    void updateDeviceStatus();
    void setupDeviceMonitorConnections();
    void showAudioSettings(); // 显示音频刺激设置对话框
    void showAnalysisSettings(); // 显示算法分析设置对话框

    //CSV处理按钮
    QPushButton *m_processCSVButton;
    QProgressBar *m_csvProgressBar;
    QLabel *m_csvProgressLabel;

    //QSS样式表支持
    void loadStyleSheet();
    void applyTheme();
    QString m_styleSheet;

    ECGDeviceThread* m_ecgThread;
    GSRDeviceThread* m_gsrThread;

    // 新增方法
    void setupDeviceThreads();

private slots:
    void onConnectDevices();
    void onDisconnectDevices(); // 新增断开设备槽函数
    bool onStartAcquisition();
    void onStopAcquisition();
    void onDeviceConnectionStatusChanged(bool connected);
    void onDeviceAcquisitionStatusChanged(bool acquiring);
    void onDeviceErrorOccurred(const QString &errorMessage);
    void onSubjectInfoCollected(int subjectId); // 处理被试信息采集完成
    void onNewSubject(); // 处理新增被试
    void onSelectSubject(); // 处理选择被试
    void onSwitchToInterventionModule(); // 处理切换到智能干预模块
    void onAudioEvent(const QString &event, qint64 timestamp);
    void onStimulusStateChanged(StimulusState state);
    void onStimulusProgressChanged(int progress);
    void onDeviceStatusClicked(const QString &deviceName);
    void onProcessCSVFile();
    void onCSVProcessingProgress(int percentage);
    void onCSVProcessingFinished(bool success, const QString& message);
    void showChatDialog();
    void onGSRSamplingRateChanged(QAction *action);
};
#endif // MAINWINDOW_H
